package com.java.simple.study.juc.p2;

import lombok.extern.slf4j.Slf4j;

/**
 * @author zhoujunlin
 * @date 2022年04月05日 17:32
 * @desc
 */
@Slf4j(topic = "c.TestYield")
public class TestYield {

    public static void main(String[] args) {
        Runnable task1 = () -> {
            int count = 0;
            for (; ; ) {
                System.out.println("---------->1  " + count++);
            }
        };

        Runnable task2 = () -> {
            int count = 0;
            for (; ; ) {
                // 一般用于测试 让cpu重新调度 CPU可以再次调用当前线程
                //Thread.yield();
                System.out.println("        ---------->2 " + count++);
            }
        };

        Thread t1 = new Thread(task1, "t1");
        Thread t2 = new Thread(task2, "t2");

        // 在CPU很忙时添加优先级才会有作用 时间片让给t2多
        t1.setPriority(Thread.MIN_PRIORITY);
        t2.setPriority(Thread.MAX_PRIORITY);

        // 未加优先级 count数大小差不多
        t1.start();
        t2.start();
    }

}
